package com.ruoyi.business.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ruoyi.business.domain.*;
import com.ruoyi.business.dto.EnterpriseVideoRecordDTO;
import com.ruoyi.business.mapper.*;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.framework.web.service.TokenService;
import lombok.Synchronized;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.service.IEnterpriseVideoRecordService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

/**
 * 企业观看视频记录Service业务层处理
 * 
 * @author lcz
 * @date 2025-01-19
 */
@Service
public class EnterpriseVideoRecordServiceImpl implements IEnterpriseVideoRecordService 
{
    @Autowired
    private EnterpriseVideoRecordMapper enterpriseVideoRecordMapper;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private VideoMapper videoMapper;

    @Autowired
    private IntegralDetailMapper integralDetailMapper;

    @Autowired
    private EnterpriseMapper enterpriseMapper;

    @Autowired
    private IntegralSettingMapper integralSettingMapper;

    /**
     * 查询企业观看视频记录
     * 
     * @param id 企业观看视频记录主键
     * @return 企业观看视频记录
     */
    @Override
    public EnterpriseVideoRecord selectEnterpriseVideoRecordById(Long id)
    {
        return enterpriseVideoRecordMapper.selectEnterpriseVideoRecordById(id);
    }

    /**
     * 查询企业观看视频记录列表
     * 
     * @param enterpriseVideoRecord 企业观看视频记录
     * @return 企业观看视频记录
     */
    @Override
    public List<EnterpriseVideoRecord> selectEnterpriseVideoRecordList(EnterpriseVideoRecord enterpriseVideoRecord)
    {
        return enterpriseVideoRecordMapper.selectEnterpriseVideoRecordList(enterpriseVideoRecord);
    }

    /**
     * 新增企业观看视频记录
     * 
     * @param enterpriseVideoRecord 企业观看视频记录
     * @return 结果
     */
    @Override
    public int insertEnterpriseVideoRecord(EnterpriseVideoRecord enterpriseVideoRecord)
    {
        enterpriseVideoRecord.setCreateTime(DateUtils.getNowDate());
        return enterpriseVideoRecordMapper.insertEnterpriseVideoRecord(enterpriseVideoRecord);
    }

    /**
     * 修改企业观看视频记录
     * 
     * @param enterpriseVideoRecord 企业观看视频记录
     * @return 结果
     */
    @Override
    public int updateEnterpriseVideoRecord(EnterpriseVideoRecord enterpriseVideoRecord)
    {
        enterpriseVideoRecord.setUpdateTime(DateUtils.getNowDate());
        return enterpriseVideoRecordMapper.updateEnterpriseVideoRecord(enterpriseVideoRecord);
    }

    /**
     * 批量删除企业观看视频记录
     * 
     * @param ids 需要删除的企业观看视频记录主键
     * @return 结果
     */
    @Override
    public int deleteEnterpriseVideoRecordByIds(Long[] ids)
    {
        return enterpriseVideoRecordMapper.deleteEnterpriseVideoRecordByIds(ids);
    }

    /**
     * 删除企业观看视频记录信息
     * 
     * @param id 企业观看视频记录主键
     * @return 结果
     */
    @Override
    public int deleteEnterpriseVideoRecordById(Long id)
    {
        return enterpriseVideoRecordMapper.deleteEnterpriseVideoRecordById(id);
    }

    @Transactional
    @Synchronized
    @Override
    public AjaxResult frontAdd(EnterpriseVideoRecordDTO dto) {
        Long id = tokenService.getUserId();

        EnterpriseVideoRecord param = new EnterpriseVideoRecord();
        param.setVideoId(dto.getVideoId());
        param.setEnterpriseId(id);

        EnterpriseVideoRecord res = enterpriseVideoRecordMapper.selectOneByMaxId(param);
        EnterpriseVideoRecord resDate = enterpriseVideoRecordMapper.selectOneByMaxIdDate(param);
        Video video = videoMapper.selectVideoById(dto.getVideoId());

        if (!dto.getWatchPercentage().toString().equals("100")){

            Long bfb = (100*dto.getWatchDuration())/video.getDuration();
            dto.setWatchPercentage(bfb);
            if (!ObjectUtils.isEmpty(res)){
                res.setWatchDuration(dto.getWatchDuration());
                res.setWatchPercentage(dto.getWatchPercentage());
                res.setUpdateTime(new Date());
                enterpriseVideoRecordMapper.updateEnterpriseVideoRecord(res);
            }else {
                EnterpriseVideoRecord enterpriseVideoRecord = new EnterpriseVideoRecord();
                BeanUtils.copyProperties(dto,enterpriseVideoRecord);
                enterpriseVideoRecord.setEnterpriseId(id);
                enterpriseVideoRecord.setCreateTime(new Date());
                enterpriseVideoRecord.setUpdateTime(new Date());
                enterpriseVideoRecordMapper.insertEnterpriseVideoRecord(enterpriseVideoRecord);
            }
        }else {
            if (!ObjectUtils.isEmpty(resDate)){
                if (!ObjectUtils.isEmpty(res)){
                    res.setWatchDuration(dto.getWatchDuration());
                    res.setWatchPercentage(dto.getWatchPercentage());
                    res.setUpdateTime(new Date());
                    enterpriseVideoRecordMapper.updateEnterpriseVideoRecord(res);
                }else {
                    EnterpriseVideoRecord enterpriseVideoRecord = new EnterpriseVideoRecord();
                    BeanUtils.copyProperties(dto,enterpriseVideoRecord);
                    enterpriseVideoRecord.setEnterpriseId(id);
                    enterpriseVideoRecord.setCreateTime(new Date());
                    enterpriseVideoRecord.setUpdateTime(new Date());
                    enterpriseVideoRecordMapper.insertEnterpriseVideoRecord(enterpriseVideoRecord);
                }
            }else {
                if (!ObjectUtils.isEmpty(res)){
                    res.setWatchDuration(dto.getWatchDuration());
                    res.setWatchPercentage(dto.getWatchPercentage());
                    res.setUpdateTime(new Date());
                    enterpriseVideoRecordMapper.updateEnterpriseVideoRecord(res);
                }else {
                    EnterpriseVideoRecord enterpriseVideoRecord = new EnterpriseVideoRecord();
                    BeanUtils.copyProperties(dto,enterpriseVideoRecord);
                    enterpriseVideoRecord.setEnterpriseId(id);
                    enterpriseVideoRecord.setCreateTime(new Date());
                    enterpriseVideoRecord.setUpdateTime(new Date());
                    enterpriseVideoRecordMapper.insertEnterpriseVideoRecord(enterpriseVideoRecord);
                }
                IntegralSetting integralSetting = integralSettingMapper.selectIntegralSettingById(1l);
                //TODO：加分
                Enterprise enterprise = enterpriseMapper.selectEnterpriseById(id);
                enterprise.setIntegral(enterprise.getIntegral()+integralSetting.getIntegralPoints());
                enterpriseMapper.updateEnterprise(enterprise);

                IntegralDetail integralDetail = new IntegralDetail();
                integralDetail.setEnterpriseId(id);
                integralDetail.setIntegral(integralSetting.getIntegralPoints());
                integralDetail.setSource("观看视频《"+video.getName()+"》获得积分");
                integralDetail.setCreateTime(new Date());
                integralDetailMapper.insertIntegralDetail(integralDetail);
            }
        }

        return AjaxResult.success();
    }
}
